  /******************************************************************************
  * @file    PT32X005_spi.h
  * @author  应用开发团队
  * @version V1.1.0
  * @date    2020/1/27
  * @brief    This file contains all the functions prototypes for the SPI firmware library.
  *
  ******************************************************************************
  * @attention
  *
  *
  *****************************************************************************/

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef PT32X005_SPI_H
#define PT32X005_SPI_H

#ifdef __cplusplus
 extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "PT32X005.h"


/** @addtogroup SPI
  * @{
  */

/* Exported types ------------------------------------------------------------*/

/** @brief  SPI Init structure definition   **/
typedef struct
{
  u16 SPI_Direction;           /*!< Specifies the SPI unidirectional or bidirectional data mode.This parameter can be a value of @ref SPI_data_direction */
  u16 SPI_Mode;                /*!< Specifies the SPI mode (Master/Slave).This parameter can be a value of @ref SPI_mode */
  u16 SPI_DataSize;            /*!< Specifies the SPI data size. This parameter can be a value of @ref SPI_data_size */
  u16 SPI_CPOL;                /*!< Specifies the serial clock steady state.This parameter can be a value of @ref SPI_Clock_Polarity */
  u16 SPI_CPHA;                /*!< Specifies the clock active edge for the bit capture.This parameter can be a value of @ref SPI_Clock_Phase */
  u16 SPI_CSS;                 /*!< Specifies whether the NSS signal is managed by hardware (NSS pin) or by software using the SSI bit. This parameter can be a value of @ref SPI_Slave_Select_management */
  u16 SPI_SWCS;
  u16 SPI_SCR;   /*!< Specifies the Baud Rate prescaler value which will be
                                         used to configure the transmit and receive SCK clock.
                                         This parameter can be a value of @ref SPI_BaudRate_Prescaler
                                         @note The communication clock is derived from the master
                                               clock. The slave clock does not need to be set. */
  u16 SPI_BR; /*!< Specifies the Baud Rate prescaler value which will be
                                         used to configure the transmit and receive SCK clock.
                                         This parameter can be a value of @ref SPI_BaudRate_Prescaler
                                         @note The communication clock is derived from the master
                                               clock. The slave clock does not need to be set. */
}SPI_InitTypeDef;


/* Exported constants --------------------------------------------------------*/

/** @defgroup SPI_Exported_Constants **/
#define IS_SPI_ALL_PERIPH(PERIPH) ((PERIPH) == SPI)

/** @defgroup SPI_Mode  **/
#define IS_SPI_MSM_SEL(SEL)	(((SEL) == SPI_CR2_MSM_Master) || \
							((SEL) == SPI_CR2_MSM_Slave))

/** @defgroup SPI_DataSize **/
#define IS_SPI_DFF_SEL(SEL)	(((SEL) == SPI_CR1_DFF_4b) || \
							((SEL) == SPI_CR1_DFF_5b) || \
							((SEL) == SPI_CR1_DFF_6b) || \
							((SEL) == SPI_CR1_DFF_7b) || \
							((SEL) == SPI_CR1_DFF_8b) || \
							((SEL) == SPI_CR1_DFF_9b) || \
							((SEL) == SPI_CR1_DFF_10b) || \
							((SEL) == SPI_CR1_DFF_11b) || \
							((SEL) == SPI_CR1_DFF_12b) || \
							((SEL) == SPI_CR1_DFF_13b) || \
							((SEL) == SPI_CR1_DFF_14b) || \
							((SEL) == SPI_CR1_DFF_15b) || \
							((SEL) == SPI_CR1_DFF_16b))

/** @defgroup SPI_CPOL **/
#define IS_SPI_CPOL_SEL(SEL)	(((SEL) == SPI_CPOL_Low) || \
								((SEL) == SPI_CPOL_High))

/** @defgroup SPI_CPHA **/
#define IS_SPI_CPHA_SEL(SEL)	(((SEL) == SPI_CPHA_1Edge) || \
								((SEL) == SPI_CPHA_2Edge))

/** @defgroup SPI_CSS  **/
#define IS_SPI_CSS_SEL(SEL)		(((SEL) == SPI_CSS_Soft) || \
								((SEL) == SPI_CSS_Hard))

/** @defgroup IS_SPI_SCR_VAL  **/
#define IS_SPI_SCR_VAL(Value) ((Value) < 256)

/** @defgroup IS_SPI_BR_VAL  **/
#define IS_SPI_BR_VAL(Value) (((Value) > 1) && ((Value) < 255))


/** @defgroup IS_SPI_SWCS_SEL  **/
#define IS_SPI_SWCS_SEL(SEL)	(((SEL) == SPI_SWCS_Low) || \
								((SEL) == SPI_SWCS_High))

/** @defgroup IS_SPI_IE_SEL&GET  **/
#define IS_SPI_IE_SEL(SEL) ((((SEL) & (u8)0xF0) == 0) && ((SEL) != (u8)0x00))
#define IS_SPI_IE_GET(SEL)	(((SEL) == SPI_IE_OVRE) || \
							((SEL) == SPI_IE_OTE) || \
							((SEL) == SPI_IE_RXHE) || \
							((SEL) == SPI_IE_TXHE))

/** @defgroup IS_SPI_IFC_SEL  **/
#define IS_SPI_IFC_SEL(SEL) ((((SEL) & (u8)0xFC) == 0) && ((SEL) != (u8)0x00))

typedef enum
{
	SPI_FLAG_TXE = 0,
	SPI_FLAG_TNF,
	SPI_FLAG_RXNE,
	SPI_FLAG_RXF,
	SPI_FLAG_BSY,
	SPI_FLAG_OVR,
	SPI_FLAG_OT,
	SPI_FLAG_RXH,
	SPI_FLAG_TXH
} SPI_Flag_Enum;

/** @defgroup IS_SPI_SR1_SEL  **/
#define IS_SPI_SR_SEL(SEL)	(((SEL) == SPI_FLAG_TXE) || \
							((SEL) == SPI_FLAG_TNF) || \
							((SEL) == SPI_FLAG_RXNE) || \
							((SEL) == SPI_FLAG_RXF) || \
							((SEL) == SPI_FLAG_BSY) || \
							((SEL) == SPI_FLAG_OVR) || \
							((SEL) == SPI_FLAG_OT) || \
							((SEL) == SPI_FLAG_RXH) || \
							((SEL) == SPI_FLAG_TXH))

#define	SPI_CS_HIGH()	SPI_CSInternalSoftwareConfig(SPI, SPI_SWCS_High)
#define	SPI_CS_LOW()	SPI_CSInternalSoftwareConfig(SPI, SPI_SWCS_Low)

/* Exported macro ------------------------------------------------------------*/
/* Exported functions ------------------------------------------------------- */
void SPI_Init(SPI_TypeDef* SPIx, SPI_InitTypeDef* SPI_InitStruct);
void SPI_StructInit(SPI_InitTypeDef* SPI_InitStruct);
void SPI_Cmd(SPI_TypeDef* SPIx, FunctionalState NewState);
void SPI_CSInternalSoftwareConfig(SPI_TypeDef* SPIx, u16 SPI_SWCS_SEL);
void SPI_SendData(SPI_TypeDef* SPIx, u16 Data);
u16 SPI_ReceiveData(SPI_TypeDef* SPIx);
void SPI_ITConfig(SPI_TypeDef* SPIx, u8 SPI_IT, FunctionalState NewState);
FlagStatus SPI_GetFlagStatus(SPI_TypeDef* SPIx, u16 SPI_FLAG);
void SPI_ClearFlag(SPI_TypeDef* SPIx, u16 SPI_FLAG);


/**
  * @}
  */


#ifdef __cplusplus
}
#endif

#endif


